<template>
  <el-dropdown @command="handleCommand" trigger="click">
    <div class="user-avatar">
      <el-avatar :size="40" :src="avatarUrl">
        {{ currentUser ? currentUser.username.charAt(0).toUpperCase() : 'U' }}
      </el-avatar>
      <div class="user-info">
        <span class="username">{{ currentUser ? currentUser.username : '用户' }}</span>
        <span class="role">{{ getRoleName(currentUser?.role) }}</span>
      </div>
      <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="profile">
          <el-icon><User /></el-icon>
          <span>个人资料</span>
        </el-dropdown-item>
        <el-dropdown-item command="settings">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </el-dropdown-item>
        <el-dropdown-item divided command="logout">
          <el-icon><SwitchButton /></el-icon>
          <span>退出登录</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown, User, Setting, SwitchButton } from '@element-plus/icons-vue'

export default {
  name: 'LogoutButton',
  components: {
    ArrowDown,
    User,
    Setting,
    SwitchButton
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const currentUser = computed(() => store.getters.currentUser)
    const avatarUrl = computed(() => currentUser.value?.avatar || '')
    
    const getRoleName = (role) => {
      const roleNames = {
        'admin': '管理员',
        'teacher': '教师',
        'student': '学生'
      }
      return roleNames[role] || '未知'
    }
    
    const handleCommand = (command) => {
      if (command === 'logout') {
        store.dispatch('logout')
        router.push('/login')
        ElMessage.success('已成功退出登录')
      } else if (command === 'profile') {
        router.push('/dashboard')
      } else if (command === 'settings') {
        ElMessage.info('系统设置功能开发中...')
      }
    }
    
    return {
      currentUser,
      avatarUrl,
      getRoleName,
      handleCommand
    }
  }
}
</script>

<style scoped>
.user-avatar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 25px;
  background: #f8f9fa;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #e4e7ed;
}

.user-avatar:hover {
  background: #e9ecef;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.username {
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
  line-height: 1.2;
}

.role {
  font-size: 12px;
  color: #7f8c8d;
  line-height: 1.2;
}

.dropdown-icon {
  color: #7f8c8d;
  font-size: 12px;
  transition: transform 0.3s ease;
}

.user-avatar:hover .dropdown-icon {
  transform: rotate(180deg);
}

.el-dropdown-menu__item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.el-dropdown-menu__item .el-icon {
  font-size: 16px;
}

@media (max-width: 768px) {
  .user-info {
    display: none;
  }
  
  .user-avatar {
    padding: 8px;
  }
}
</style> 